﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserService
    {
        /// <summary>
        /// 根据用户名查找用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static User GetUser(string userName)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.User.FirstOrDefault(p => p.UserName == userName);
            }
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="UserName"></param>
        /// <param name="Sex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<User> GetUserMes(int limit, int page, string UserName, string Sex, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.User.Where(p => true);
                if (!string.IsNullOrEmpty(UserName))
                {
                    query = query.Where(p => p.UserName.Contains(UserName));
                }
                if (!string.IsNullOrEmpty(Sex))
                {
                    query = query.Where(p => p.Sex == Sex);
                }
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int GetAdd(string UserName, string PassWord, string Sex)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                User user = new User
                {
                    UserName = UserName,
                    Pwd = PassWord,
                    Sex = Sex
                };
                db.User.Add(user);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 编辑用户数据
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="RealName"></param>
        /// <param name="Age"></param>
        /// <param name="Mobile"></param>
        /// <param name="realId"></param>
        /// <returns></returns>
        public static int GetUpdate(string UserName, string Sex, int realId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                User userMes = db.User.SingleOrDefault(p => p.Id == realId);
                userMes.UserName = UserName;
                userMes.Sex = Sex;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetDel(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                User user = new User() { Id = id };
                db.User.Attach(user);
                db.User.Remove(user);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static int GetBatchDelByIds(string ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = ids.Substring(0, ids.Length - 1);
                string sql = $"delete [User] where Id in ({newids})";
                return db.Database.ExecuteSqlCommand(sql);
            }

        }


        /// <summary>
        /// 根据id查找数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static User GetSelec(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.User.FirstOrDefault(p => p.Id == id);
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="NewPassWord"></param>
        /// <returns></returns>
        public static int GetUpdatePwd(string UserName, string NewPassWord)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                User userMes = db.User.SingleOrDefault(p => p.UserName == UserName);
                userMes.Pwd = NewPassWord;
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetRestPwd(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                User userMes = db.User.SingleOrDefault(p => p.Id == id);
                userMes.Pwd = "e10adc3949ba59abbe56e057f20f883e";
                return db.SaveChanges();
            }
        }

    }
}
